package me.zhengjie.modules.quartz.utils

import me.zhengjie.common.utils.SpringContextHolder
import org.apache.commons.lang3.StringUtils
import org.springframework.util.ReflectionUtils
import java.lang.reflect.Method
import java.util.concurrent.Callable

/**
 * 执行定时任务
 * @author /
 */
class QuartzRunnable internal constructor(beanName: String?, methodName: String?, private val params: String) :
    Callable<Any?> {
    private val target: Any
    private var method: Method? = null

    init {
        target = SpringContextHolder.Companion.getBean<Any>(beanName)
        if (StringUtils.isNotBlank(params)) {
            method = target.javaClass.getDeclaredMethod(methodName, String::class.java)
        } else {
            method = target.javaClass.getDeclaredMethod(methodName)
        }
    }

    @Throws(Exception::class)
    override fun call(): Any? {
        ReflectionUtils.makeAccessible(method!!)
        if (StringUtils.isNotBlank(params)) {
            method!!.invoke(target, params)
        } else {
            method!!.invoke(target)
        }
        return null
    }
}